package com.yidiandao.business.ui.widget.recyclerview.provider;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.data.bean.Tag;
import com.yidiandao.business.data.recyclertype.SearchHistory;
import com.yidiandao.business.ui.widget.view.TagFlowLayout;
import com.yidiandao.business.ui.widget.view.TagView;

import java.util.ArrayList;
import java.util.List;

import me.drakeet.multitype.ItemViewProvider;

/**
 * 发现页-搜索历史
 * <p>
 * Created by ChenTao(chentao7v@gmail.com) on 2017/4/13 0013 9:00
 */
public class DiscoverySearchHistoryProvider extends ItemViewProvider<SearchHistory, DiscoverySearchHistoryProvider.ViewHolder> {

    private OnHistoryItemClickListener mListener;

    public DiscoverySearchHistoryProvider(OnHistoryItemClickListener listener) {
        mListener = listener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_discovery_search_history, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull SearchHistory searchHistory) {

        List<String> history = searchHistory.getHistory();

        List<Tag> tags = new ArrayList<>();

        for (int i = 0; i < history.size(); i++) {
            tags.add(new Tag(history.get(i)));
        }

        holder.listener = mListener;
        holder.tagFlowLayout.setTagState(TagView.DISCOVERY);
        holder.tagFlowLayout.setTagsData(tags);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tvClearHistory;

        private TagFlowLayout tagFlowLayout;

        public OnHistoryItemClickListener listener;

        public ViewHolder(View itemView) {
            super(itemView);

            tvClearHistory = (TextView) itemView.findViewById(R.id.tv_clear_history);
            tagFlowLayout = (TagFlowLayout) itemView.findViewById(R.id.tag_flow_layout);

            tagFlowLayout.setTagListener(new TagFlowLayout.TagListener() {
                @Override
                public void onTagClick(TagView tagView) {
                    if (listener != null) {
                        listener.onTagClick(tagView.getTagDataName());
                    }
                }

                @Override
                public void onTextChanged(String text) {

                }
            });

            tvClearHistory.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onClearHistory();
                    }
                }
            });
        }
    }

    public interface OnHistoryItemClickListener {

        void onClearHistory();

        void onTagClick(String tag);
    }
}
